﻿using System;


[AttributeUsage(AttributeTargets.All, Inherited = true)]
//[AttributeUsage(AttributeTargets.All,AllowMultiple = true)]//AllowMultiple = true用于开启重复特性
public class HelpAttribute : System.Attribute
{
    public readonly string url;

    string _topic;
    public string Topic
    {
        get { return _topic; }
        set { _topic = value; }
    }

    public HelpAttribute(string url) { this.url = url; }
}

//[HelpAttribute("Informatin on the class MyClass")]
[HelpAttribute("Informatin on the class MyClass1")]//这里不是继承，而是反射
//[HelpAttribute("Informatin on the class MyClass2")]//只有开启重复特性，才能有多行
class MyClass {
    public string murl;
    public MyClass() => murl = "MyClass.murl";
}

namespace 反射1
{
    class Program
    {
        static void Main(string[] args)
        {
            //System.Reflection 类的 MemberInfo 对象需要被初始化，用于发现与类相关的特性（attribute）。
            System.Reflection.MemberInfo info = typeof(MyClass);

            object[] attributes = info.GetCustomAttributes(true);
            for(int i = 0; i < attributes.Length; i++)
            {
                System.Console.WriteLine(attributes[i]);
                System.Console.WriteLine("Url={0}", ((HelpAttribute)attributes[i]).url);
                //下面这句错误
                //提示：无法将类型为“HelpAttribute”的对象强制转换为类型“MyClass”
                System.Console.WriteLine("MyClass.Url={0}", ((MyClass)attributes[i]).url);
            }

            /*MyClass myClass = new MyClass();
            Console.WriteLine(myClass.murl);
            Console.WriteLine(myClass.GetType());*/
            Console.ReadKey();
        }
    }
}
